﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using MYNAMESPACE;

namespace final_project
{
    public partial class Machine_login : Form
    {

         private connect_db con;
        public string nm;
        public string passvalue
        {

            get
            {
                return nm;
            }
            set
            {
                nm = value;
            }
        }
        public Machine_login()
        {
            InitializeComponent();
        }

        private void Machine_login_Load(object sender, EventArgs e)
        {
            CO_ID.Text = nm;

        }

        private void button1_Click(object sender, EventArgs e)
        {

            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 
            
            user US = new user();
            US.setnational_id(Int32.Parse(ID.Text));
            US.setpassword(string.Format(Password.Text));
            if (ID.Text.Length == null || Password.Text.Length == null)
            {

                //this.Hide();
                MessageBox.Show("must have value");
                //Form1 f1 = new Form1();
                //f1.ShowDialog();

            }
            else
            {
                user usc = new user();
                usc.setnational_id(Int32.Parse(CO_ID.Text));
                if (US.getnational_id() !=  usc.getnational_id())
                {
               
                con = new connect_db();
                con.sqlquery("select * from userdata where id= (@namep) and pass=(@namep1)");
                con.cmd.Parameters.Add("@namep",US.getnational_id());
                con.cmd.Parameters.Add("@namep1", US.getpassword());
                con.sqlnonex();
                int c = 0;
                foreach (DataRow dr in con.QueryEx().Rows)
                {
                    c = c + 1;
                }
                if (c == 1)
                {
                    this.Hide();
                    BAYMACHIN BA = new BAYMACHIN();
                    BA.passvalue = ID.Text;
                    BA.passvalue1 = CO_ID.Text;
                    BA.ShowDialog();
                }
                else
                {
                    MessageBox.Show("not correct");

                }
                }
                else
                {

                    MessageBox.Show("Errror");
                }
              




            }
        }

        private void ID_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(ID.Text))
            {

                e.Cancel = true;
                ID.Focus();
                errorProvider1.SetError(ID, "ID Must Be Entered !");

            }

            else if (!Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(ID, "Valid ID 4 Number");

            }



            else if (Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(ID, "");

            }
            else { }


        }

        private void Password_Validating(object sender, CancelEventArgs e)
        {


            if (string.IsNullOrEmpty(Password.Text))
            {

                e.Cancel = true;
                Password.Focus();
                errorProvider1.SetError(Password, "Password Must Be Entered !");
               
            }
            else if (!Regex.IsMatch(Password.Text, @"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!*@#$%^&+=]).*$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(Password, "Password Must more Than 8 Digits Contain upper-lowercase,numbers and special characters");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(Password, "");
            }

            


        }
    }
}
